using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;

using ChatLib;
using System.Runtime.Remoting;
using System.Runtime.Remoting.Channels;
using System.Runtime.Remoting.Channels.Http;
using System.Threading;

namespace Chat
{
	public class FormSignIn : System.Windows.Forms.Form
	{
		private System.Windows.Forms.GroupBox groupBox1;
		private System.Windows.Forms.Button btnCreateAccount;
		internal System.Windows.Forms.GroupBox groupBox2;
		private System.Windows.Forms.Label label1;
		private System.Windows.Forms.Label label2;
		internal System.Windows.Forms.TextBox txtUsername;
		private System.Windows.Forms.TextBox txtPassword;
		private System.Windows.Forms.Button btnSignIn;
		private System.Windows.Forms.Button btnCancel;
		private System.ComponentModel.IContainer components;
		private System.Windows.Forms.CheckBox chkInvisible;
		private System.Windows.Forms.Button btnChangePassword;

		public FormSignIn()
		{
			//
			// Required for Windows Form Designer support
			//
			InitializeComponent();

			//
			// TODO: Add any constructor code after InitializeComponent call
			//
		}

		/// <summary>
		/// Clean up any resources being used.
		/// </summary>
		protected override void Dispose( bool disposing )
		{
			if( disposing )
			{
				if(components != null)
				{
					components.Dispose();
				}
			}
			base.Dispose( disposing );
		}

		#region Windows Form Designer generated code
		/// <summary>
		/// Required method for Designer support - do not modify
		/// the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{
			System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(FormSignIn));
			this.groupBox1 = new System.Windows.Forms.GroupBox();
			this.btnCreateAccount = new System.Windows.Forms.Button();
			this.groupBox2 = new System.Windows.Forms.GroupBox();
			this.chkInvisible = new System.Windows.Forms.CheckBox();
			this.txtPassword = new System.Windows.Forms.TextBox();
			this.txtUsername = new System.Windows.Forms.TextBox();
			this.label2 = new System.Windows.Forms.Label();
			this.label1 = new System.Windows.Forms.Label();
			this.btnSignIn = new System.Windows.Forms.Button();
			this.btnCancel = new System.Windows.Forms.Button();
			this.btnChangePassword = new System.Windows.Forms.Button();
			this.groupBox1.SuspendLayout();
			this.groupBox2.SuspendLayout();
			this.SuspendLayout();
			// 
			// groupBox1
			// 
			this.groupBox1.Controls.Add(this.btnCreateAccount);
			this.groupBox1.Location = new System.Drawing.Point(8, 8);
			this.groupBox1.Name = "groupBox1";
			this.groupBox1.Size = new System.Drawing.Size(256, 48);
			this.groupBox1.TabIndex = 0;
			this.groupBox1.TabStop = false;
			this.groupBox1.Text = "Don\'t have an account";
			// 
			// btnCreateAccount
			// 
			this.btnCreateAccount.FlatStyle = System.Windows.Forms.FlatStyle.System;
			this.btnCreateAccount.Location = new System.Drawing.Point(64, 16);
			this.btnCreateAccount.Name = "btnCreateAccount";
			this.btnCreateAccount.Size = new System.Drawing.Size(120, 24);
			this.btnCreateAccount.TabIndex = 0;
			this.btnCreateAccount.Text = "Create Account";
			this.btnCreateAccount.Click += new System.EventHandler(this.btnCreateAccount_Click);
			// 
			// groupBox2
			// 
			this.groupBox2.Controls.Add(this.chkInvisible);
			this.groupBox2.Controls.Add(this.txtPassword);
			this.groupBox2.Controls.Add(this.txtUsername);
			this.groupBox2.Controls.Add(this.label2);
			this.groupBox2.Controls.Add(this.label1);
			this.groupBox2.Location = new System.Drawing.Point(8, 64);
			this.groupBox2.Name = "groupBox2";
			this.groupBox2.Size = new System.Drawing.Size(256, 112);
			this.groupBox2.TabIndex = 1;
			this.groupBox2.TabStop = false;
			this.groupBox2.Text = "Already have an Account";
			// 
			// chkInvisible
			// 
			this.chkInvisible.FlatStyle = System.Windows.Forms.FlatStyle.System;
			this.chkInvisible.Location = new System.Drawing.Point(72, 88);
			this.chkInvisible.Name = "chkInvisible";
			this.chkInvisible.Size = new System.Drawing.Size(112, 16);
			this.chkInvisible.TabIndex = 2;
			this.chkInvisible.Text = "Sign In as Invisible";
			// 
			// txtPassword
			// 
			this.txtPassword.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
			this.txtPassword.Location = new System.Drawing.Point(72, 56);
			this.txtPassword.Name = "txtPassword";
			this.txtPassword.PasswordChar = '*';
			this.txtPassword.Size = new System.Drawing.Size(160, 20);
			this.txtPassword.TabIndex = 1;
			// 
			// txtUsername
			// 
			this.txtUsername.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
			this.txtUsername.ForeColor = System.Drawing.SystemColors.WindowText;
			this.txtUsername.Location = new System.Drawing.Point(72, 24);
			this.txtUsername.Name = "txtUsername";
			this.txtUsername.Size = new System.Drawing.Size(160, 20);
			this.txtUsername.TabIndex = 0;
			// 
			// label2
			// 
			this.label2.Location = new System.Drawing.Point(8, 56);
			this.label2.Name = "label2";
			this.label2.Size = new System.Drawing.Size(64, 16);
			this.label2.TabIndex = 1;
			this.label2.Text = "Password :";
			// 
			// label1
			// 
			this.label1.Location = new System.Drawing.Point(8, 24);
			this.label1.Name = "label1";
			this.label1.Size = new System.Drawing.Size(64, 16);
			this.label1.TabIndex = 0;
			this.label1.Text = "Username :";
			// 
			// btnSignIn
			// 
			this.btnSignIn.DialogResult = System.Windows.Forms.DialogResult.OK;
			this.btnSignIn.FlatStyle = System.Windows.Forms.FlatStyle.System;
			this.btnSignIn.Location = new System.Drawing.Point(8, 184);
			this.btnSignIn.Name = "btnSignIn";
			this.btnSignIn.Size = new System.Drawing.Size(64, 24);
			this.btnSignIn.TabIndex = 0;
			this.btnSignIn.Text = "Sign In";
			this.btnSignIn.Click += new System.EventHandler(this.btnSignIn_Click);
			// 
			// btnCancel
			// 
			this.btnCancel.DialogResult = System.Windows.Forms.DialogResult.Cancel;
			this.btnCancel.FlatStyle = System.Windows.Forms.FlatStyle.System;
			this.btnCancel.Location = new System.Drawing.Point(80, 184);
			this.btnCancel.Name = "btnCancel";
			this.btnCancel.Size = new System.Drawing.Size(64, 24);
			this.btnCancel.TabIndex = 1;
			this.btnCancel.Text = "Cancel";
			// 
			// btnChangePassword
			// 
			this.btnChangePassword.FlatStyle = System.Windows.Forms.FlatStyle.System;
			this.btnChangePassword.Location = new System.Drawing.Point(152, 184);
			this.btnChangePassword.Name = "btnChangePassword";
			this.btnChangePassword.Size = new System.Drawing.Size(112, 24);
			this.btnChangePassword.TabIndex = 2;
			this.btnChangePassword.Text = "Change Password";
			this.btnChangePassword.Click += new System.EventHandler(this.btnChangePassword_Click);
			// 
			// FormSignIn
			// 
			this.AcceptButton = this.btnSignIn;
			this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
			this.CancelButton = this.btnCancel;
			this.ClientSize = new System.Drawing.Size(272, 216);
			this.Controls.Add(this.btnCancel);
			this.Controls.Add(this.btnSignIn);
			this.Controls.Add(this.groupBox2);
			this.Controls.Add(this.groupBox1);
			this.Controls.Add(this.btnChangePassword);
			this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedSingle;
			this.Icon = ((System.Drawing.Icon)(resources.GetObject("$this.Icon")));
			this.MaximizeBox = false;
			this.MinimizeBox = false;
			this.Name = "FormSignIn";
			this.ShowInTaskbar = false;
			this.StartPosition = System.Windows.Forms.FormStartPosition.CenterParent;
			this.Text = "Sign In";
			this.Closing += new System.ComponentModel.CancelEventHandler(this.FormSignIn_Closing);
			this.groupBox1.ResumeLayout(false);
			this.groupBox2.ResumeLayout(false);
			this.groupBox2.PerformLayout();
			this.ResumeLayout(false);

		}
		#endregion

		private void btnCreateAccount_Click(object sender, System.EventArgs e)
		{
			FormCreateAccount frmCreateAccount = new FormCreateAccount();
			if(frmCreateAccount.ShowDialog()==DialogResult.OK)
			{
				txtUsername.Text=frmCreateAccount.txtUsername.Text;
				txtPassword.Text=frmCreateAccount.txtPassword.Text;
			}
		}
		private void btnSignIn_Click(object sender, System.EventArgs e)
		{
			;
			try
			{
				if(Global.server.SignIn(txtUsername.Text,txtPassword.Text,!chkInvisible.Checked))
				{

				}
				else
				{
					MessageBox.Show("Your Username and password do not match. Please check noth");
					this.DialogResult=DialogResult.None;
				}
			}
			catch
			{
				MessageBox.Show("Count not connect to the server. Please check your network settings");
				this.DialogResult=DialogResult.Cancel;
			}
		}

		private void FormSignIn_Closing(object sender, System.ComponentModel.CancelEventArgs e)
		{
			if(this.DialogResult==DialogResult.None)
			{
				e.Cancel=true;
			}
		}

		private void btnChangePassword_Click(object sender, System.EventArgs e)
		{
			FormChangePassword frmChangePassword = new FormChangePassword();
			frmChangePassword.txtUsername.Text=txtUsername.Text;
			frmChangePassword.txtCurPassword.Text=txtPassword.Text;
			if(frmChangePassword.ShowDialog()==DialogResult.OK)
			{
				txtUsername.Text=frmChangePassword.txtUsername.Text;
				txtPassword.Text=frmChangePassword.txtNewPassword.Text;
			}
		}
	}
}
